package com.utils.result;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author xufan
 */
public class Ret<T> {
    private static final Logger logger = LoggerFactory.getLogger(Ret.class);
    private String code;
    private String msg;
    private String status;
    private T result;

    @Override
    public String toString() {
        return "status=" + status + "code=" + code + " message=" + msg + " data=" + result;
    }
    /**
     *  @author: xufan
     *  @Date: 2021/8/5
     *  @Description: 未登录
     */
    public static <T> Ret<T> failNoLogin(String msg) {
        Ret<T> ret = new Ret<T>();
        ret.setStatus(ReturnCode.NOT_LOGIN.getStatus());
        ret.setCode(ReturnCode.NOT_LOGIN.getCode());
        ret.setMsg(msg);
        return ret;
    }
    /**
     *  @author: xufan
     *  @Date: 2021/8/5
     *  @Description: 接口错误-自定义描述语
     */
    public static <T> Ret<T> fail(String msg) {
        Ret<T> ret = new Ret<T>();
        ret.setStatus(ReturnCode.FAIL.getStatus());
        ret.setCode(ReturnCode.FAIL.getCode());
        ret.setMsg(msg);
        return ret;
    }

    /**
     *  @author: xufan
     *  @Date: 2021/8/5
     *  @Description: 接口错误
     */
    public static <T> Ret<T> fail() {
        Ret<T> ret = new Ret<T>();
        ret.setStatus(ReturnCode.FAIL.getStatus());
        ret.setCode(ReturnCode.FAIL.getCode());
        ret.setMsg(ReturnCode.FAIL.getDesc());
        return ret;
    }
    /**
     *  @author: xufan
     *  @Date: 2021/8/5
     *  @Description: 接口异常-对象处理类
     */
    public static <T> Ret<T> fail(T data) {
        Ret<T> ret = Ret.fail();
        ret.setResult(data);
        return ret;
    }
    /**
     *  @author: xufan
     *  @Date: 2021/8/5
     *  @Description: 接口成功
     */
    public static <T> Ret<T> success() {
        Ret<T> ret = new Ret<T>();
        ret.setStatus(ReturnCode.SUCCESS.getStatus());
        ret.setCode(ReturnCode.SUCCESS.getCode());
        ret.setMsg(ReturnCode.SUCCESS.getDesc());
        return ret;
    }

    /**
     *  @author: xufan
     *  @Date: 2021/8/5 
     *  @Description: 接口成功 -对象
     */
    public static <T> Ret<T> success(T data) {
        Ret<T> ret = Ret.success();
        ret.setResult(data);
        return ret;
    }
    /**
     *  @author: xufan
     *  @Date: 2021/8/5 
     *  @Description: 接口找不到
     */
    public static <T> Ret<T> http404(T data) {
        Ret<T> ret = new Ret<T>();
        ret.setStatus(ReturnCode.NOT_FOUND.getStatus());
        ret.setCode(ReturnCode.NOT_FOUND.getCode());
        ret.setMsg(ReturnCode.NOT_FOUND.getDesc());
        ret.setResult(data);
        return ret;
    }
    /**
     *  @author: xufan
     *  @Date: 2021/8/5
     *  @Description: 拒绝访问
     */
    public static <T> Ret<T> http403(T data) {
        Ret<T> ret = new Ret<T>();
        ret.setStatus(ReturnCode.ACCESS_ERROR.getStatus());
        ret.setCode(ReturnCode.ACCESS_ERROR.getCode());
        ret.setMsg(ReturnCode.ACCESS_ERROR.getDesc());
        ret.setResult(data);
        return ret;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
